<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="110px">
      <el-form-item label="请求单号" prop="requestNo">
        <el-input
          v-model="queryParams.requestNo"
          placeholder="请输入请求单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商户类型" prop="merchantType">
        <el-select v-model="queryParams.merchantType" placeholder="请选择商户类型" clearable>
          <el-option label="企业" value="ENTERPRISE"/>
          <el-option label="个体户" value="INDIVIDUALBISS"/>
        </el-select>
      </el-form-item>
      <el-form-item label="公司名称" prop="signedName">
        <el-input
          v-model="queryParams.signedName"
          placeholder="请输入公司名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="店铺名称" prop="shopName">
        <el-input
          v-model="queryParams.shopName"
          placeholder="请输入店铺名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="法人姓名" prop="legalPerson">
        <el-input
          v-model="queryParams.legalPerson"
          placeholder="请输入法人姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="法人手机号" prop="phone">
          <el-input
            v-model="queryParams.phone"
            placeholder="请输入法人手机号"
            clearable
            @keyup.enter.native="handleQuery"
          />
      </el-form-item>
      <el-form-item label="法人邮箱" prop="contactEmail">
        <el-input
          v-model="queryParams.contactEmail"
          placeholder="请输入法人邮箱"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="电子户状态" prop="registerStatus">
        <el-select v-model="queryParams.registerStatus" placeholder="请选择电子户状态" clearable>
          <el-option
            v-for="item in statusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="银行报备状态" prop="channelStatus">
        <el-select v-model="queryParams.channelStatus" placeholder="请选择银行报备" clearable>
          <el-option
            v-for="item in channelStatusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="入网状态" prop="settledStatus">
        <el-select v-model="queryParams.settledStatus" placeholder="请选择入网状态" clearable>
          <el-option
            v-for="item in settledStatusOptions"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>

      <el-form-item label="所属平台" v-if="user.userId === 1" prop="platformNickName">
        <el-input
          v-model="queryParams.platformNickName"
          placeholder="请输入所属平台"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属代理" v-if="user.deptId < 101" prop="agentNickName">
        <el-input
          v-model="queryParams.agentNickName"
          placeholder="请输入所属代理"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="所属二级代理" v-if="user.deptId < 102" prop="childAgentNickName">
        <el-input
          v-model="queryParams.childAgentNickName"
          placeholder="请输入所属二级代理"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="所属商户" prop="shopNickName">
        <el-input
          v-model="queryParams.shopNickName"
          placeholder="请输入所属商户"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['ecs:dinPay:add']"
        >新增</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['ecs:dinPay:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="dinPayList">
      <el-table-column label="店铺名称" fixed="left" align="center" prop="shopName" width="280" />
      <el-table-column label="公司名称" align="center" prop="signedName" width="300"/>
      <el-table-column label="所属平台" v-if="user.userId === 1" align="center" prop="platformNickName"  width="210"/>
      <el-table-column label="所属代理" v-if="user.deptId < 101" align="center" prop="agentNickName"  width="210"/>
      <el-table-column label="所属二级代理" v-if="user.deptId < 102" align="center" prop="childAgentNickName"  width="210"/>
      <el-table-column label="所属商户" align="center" prop="shopNickName"  width="210"/>
      <el-table-column label="店铺简介" align="center" prop="merchantShowName" width="260"/>
      <el-table-column label="商户类型" align="center" prop="merchantType"  width="100">
        <template slot-scope="{ row }">
          {{row.merchantType==="ENTERPRISE"?"企业":"个体户"}}
        </template>
      </el-table-column>
      <el-table-column label="结算类型" align="center" prop="accountType" width="100">
        <template slot-scope="{ row }">
          {{row.accountType==="TOPUBLIC"?"对公":"对私"}}
        </template>
      </el-table-column>
      <el-table-column label="法人姓名" align="center" prop="legalPerson" width="110"/>
      <el-table-column label="法人手机号" align="center" prop="phone" width="110"/>
      <el-table-column label="联系人邮箱" align="center" prop="contactEmail" width="200"/>
      <el-table-column label="状态" fixed="right" align="center" prop="status" width="250">
        <template slot-scope="{ row }">
          电子户状态：{{row.registerStatus===0?"初始化":row.registerStatus===1?"审核驳回":row.registerStatus===2?"待验证":row.registerStatus===3?"待签约":row.registerStatus===4?"审核通过":row.registerStatus===5?"处理中":"其他"}}
          <br />
          银行报备状态：{{row.channelStatus===0?"初始化":row.channelStatus===1?"处理中":row.channelStatus===2?"失败":row.channelStatus===3?"报备成功":"其他"}}
          <br />
          入网状态：{{row.settledStatus===0?"初始化":row.settledStatus===1?"待审核":row.settledStatus===2?"申请驳回":row.settledStatus===3?"入网成功":row.settledStatus===4?"入网失败":"其他"}}
          <template v-if="row.registerStatus===4">
            <template v-if="row.settledStatus===0">
              <el-button size="mini" type="text" @click="handRegistered(row)">提交</el-button>
            </template>
          </template>
          <el-button v-if="row.settledStatus===4||row.settledStatus===2" size="mini" type="text" @click="handRegistered(row)">提交</el-button>
          <br />
          对公校验状态：{{row.bindStatus===0?"初始化":row.bindStatus===1?"处理中":row.bindStatus===2?"待验证":row.bindStatus===3?"失败":row.bindStatus===4?"绑卡成功":"其他"}}
        </template>
      </el-table-column>
      <el-table-column label="费率" fixed="right" align="center" prop="feeRate" width="110">
        <template slot-scope="{ row }">
          {{ row.feeRate }}%
          <span v-if="user.userId === 1">- {{row.rate}}%</span>
        </template>
      </el-table-column>
      <el-table-column label="开户银行号" show-overflow-tooltip align="center" prop="electronicAccountNo"  width="230"/>
      <el-table-column label="原因" show-overflow-tooltip align="center" prop="reason" width="180">
        <template slot-scope="{ row }">
          <template v-if="row.registerStatus===1">{{row.registerMsg}}</template>
          <template v-else-if="row.channelStatus===2">{{row.channelMsg}}</template>
          <template v-else-if="row.settledStatus===2 || row.settledStatus==4">{{row.settledMsg}}</template>
          <template v-else-if="row.bindStatus===3">{{row.channeMsg}}</template>
        </template>
      </el-table-column>
      <el-table-column label="操作"  fixed="right" width="200" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            :disabled="!(scope.row.registerStatus === 0 || scope.row.registerStatus === 1) && !(scope.row.settledStatus===2 || scope.row.settledStatus==4)"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['ecs:dinPay:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-more-outline"
            @click="handDetail(scope.row)"
            v-hasPermi="['ecs:dinPay:query']"
          >详情</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.registerStatus === 4 && (scope.row.bindStatus === 0 || scope.row.bindStatus === 2 || scope.row.bindStatus === 3)"
            icon="el-icon-more-outline"
            v-hasPermi="['ecs:dinPay:accountBindVerify']"
            @click="handleAccountBindVerify(scope.row)"
          >打款验证</el-button>
<!--          v-if="scope.row.settledStatus === 3"-->
          <el-button
            size="mini"
            type="text"
            icon="el-icon-more-outline"
            @click="handleProduct(scope.row)"
            v-hasPermi="['ecs:dinPay:product']"
          >产品详情</el-button>
          <el-button
            size="mini"
            type="text"
            v-if="scope.row.settledStatus===3"
            icon="el-icon-edit"
            @click="handleUpdateFeeRate(scope.row)"
            v-hasPermi="['ecs:dinPay:editFeeRate']"
          >修改费率</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <DinPayOperate v-if="open" @close="close" :formData="form" :opens="open"></DinPayOperate>
    <DinPayDetail v-if="openDetail" @close="close" :formData="form" :opens="openDetail"></DinPayDetail>
    <Qualifications v-if="qualifications"  :formData="form" @close="close" :opens="qualifications"></Qualifications>
    <AccountBindVerify v-if="bindVerifyVisible" @close="close" :formData="form" :opens="bindVerifyVisible"></AccountBindVerify>
    <DinPayProduct v-if="productVisible" @close="close" :formData="form" :opens="productVisible"></DinPayProduct>

    <el-dialog :close-on-click-modal="false" :close-on-press-escape="false" :title="'修改 '+title+' 费率'" :visible.sync="openFeeRate" width="35%" append-to-body>
      <el-form v-loading = "loading" ref="form" :model="form" label-width="110px" class="form">
        <el-form-item label="费率" prop="feeRate">
          <el-input-number :step="0.1" :min="0" :max="100" :precision="2" v-model.number="form.feeRate" placeholder="请输入费率" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listDinPay, delDinPay, registered,editFeeRate } from '@/api/ecs/dinPay'

import DinPayProduct from '@/views/ecs/incoming/dinPay/modules/product.vue'
import Qualifications from '@/views/ecs/incoming/dinPay/modules/qualifications.vue'
import DinPayOperate from '@/views/ecs/incoming/dinPay/modules/operate.vue'
import DinPayDetail from '@/views/ecs/incoming/dinPay/modules/detail.vue'
import AccountBindVerify from '@/views/ecs/incoming/dinPay/modules/AccountBindVerify.vue'

import { getUserProfile } from '@/api/system/user'
import { getAgentUserId } from '@/api/ecs/agent'

export default {
  name: "DinPay",
  components:{
    DinPayOperate,
    DinPayDetail,
    Qualifications,
    DinPayProduct,
    AccountBindVerify
  },
  data() {
    return {
      statusOptions: [
        { value: 0, label: "初始化" },
        { value: 1, label: "拒绝" },
        { value: 2, label: "待验证" },
        { value: 3, label: "待签约" },
        { value: 4, label: "审核通过" },
        { value: 5, label: "处理中" },
      ],
      channelStatusOptions: [
        { value: 0, label: "初始化" },
        { value: 1, label: "处理中" },
        { value: 2, label: "失败" },
        { value: 3, label: "报备成功" },
      ],
      settledStatusOptions:[
        { value: 0, label: "初始化" },
        { value: 1, label: "待审核" },
        { value: 2, label: "申请驳回" },
        { value: 3, label: "入网成功" },
        { value: 4, label: "入网失败" }
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 支付 抽佣入网渠道表格数据
      dinPayList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        requestNo: null,
        merchantType:null,
        shopName:null,
        signedName:null,
        legalPerson:null,
        phone:null,
        contactEmail:null,
        registerStatus:null,
        settledStatus:null,
        shopNickName:null,
        platformNickName:null,
        agentNickName:null,
        childAgentNickName:null,
        channelStatus:null,

      },
      // 表单参数
      form: {},
      user:{},
      // 表单校验
      openDetail:false,
      bindVerifyVisible:false,
      qualifications:false,
      productVisible:false,
      openFeeRate:false
    };
  },
  created() {
    getUserProfile().then(response => {
      this.user = response.data;
      if (this.user.deptId === 101){
        getAgentUserId(this.user.userId).then(response => {
          if(response.data.agentId !== 0){
            this.user.deptId = 102
          }
        });
      }
    });
    this.getList();
  },
  methods: {
    /** 查询支付 抽佣入网渠道列表 */
    getList() {
      this.loading = true;
      listDinPay(this.queryParams).then(response => {
        this.dinPayList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    close(val1,val2) {
      this.open = val1;
      this.openDetail = val1
      this.qualifications = val1
      this.bindVerifyVisible = val1
      this.productVisible = val1
      this.form = {}
      if (val2){
        this.getList()
      }
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.open = true;
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = row;
      this.open = true;
    },

    handDetail(row){
      this.form = row;
      this.openDetail = true
    },

    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$modal.confirm('是否确认删除支付 抽佣入网渠道编号为"' + ids + '"的数据项？').then(function() {
        return delDinPay(ids);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    handleAccountBindVerify(row){
      this.form = row
      this.bindVerifyVisible = true
    },
    handleQualifications(row){
      this.form = row
      this.qualifications = true
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ecs/dinPay/export', {
        ...this.queryParams
      }, `dinPay_${new Date().getTime()}.xlsx`)
    },
    handRegistered(val){
      this.loading = true;
      registered({id:val.id}).then(res=>{
        this.loading = false
        if(res.code === 200){
          this.getList();
          this.$modal.msgSuccess('提交成功')
        }
      })
    },
    handleProduct(row){
      this.form = row
      this.productVisible = true
    },
    handleUpdateFeeRate(row){
      this.openFeeRate = true;
      this.title = row.signedName
      this.form = {
        id:row.id,
        feeRate:row.feeRate
      }
    },
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          editFeeRate(this.form).then(response => {
            this.$modal.msgSuccess("修改成功");
            this.openFeeRate = false;
            this.getList();
          });
        }
      });
    },
    reset() {
      this.resetForm("form");
    },
    cancel() {
      this.openFeeRate = false;
      this.reset();
    },
  }
};
</script>
